//
//  GatherViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/2/23.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "GatherViewController.h"
#import "GatherHeadView.h"
#import "FIDPickerView.h"
#import "DoorSendCodeModel.h"
#import "AppDelegate+DTCAppService.h"
#import "AppDelegate+DTCAppLifeCycle.h"
#import "AppDelegate+DTCRootController.h"
#import "DoorStatusModel.h"
#import "RongGaoGit.h"

@interface GatherViewController ()

/** titleArray **/
@property(nonatomic,strong) NSArray *titleArray;

/** DescribeArray **/
@property(nonatomic,strong) NSArray *DescribeArray;
/** headView **/
@property(nonatomic,strong) GatherHeadView *headView;

/** PickerView **/
@property(nonatomic,strong) FIDPickerView *pickerView;

/** SendBtn **/
@property(nonatomic,strong) UIButton *sendBtn;

/** index **/
@property(nonatomic,assign) NSInteger tagIndex;

/** valueArray **/
@property(nonatomic,strong) NSMutableArray *valueArray;

@end

@implementation GatherViewController

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];

    [self readMemory];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self masLayoutSubview];
    switch (self.tagIndex) {
        case 12:
            [self setNavigationTitle:@"速度调节" titleColor:nil];
            break;
        case 13:
            [self setNavigationTitle:@"开门停留时间" titleColor:nil];
            break;
       
        default:
            [self setNavigationTitle:self.titleArray[self.tagIndex] titleColor:nil];

            break;
    }
    
    self.headView.describeLabel.text = self.DescribeArray[self.tagIndex];
    
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(readMemory) name:@"updateTableView" object:nil];

}

- (void)setGatherVCTitleAndPickerType:(TitleLabelAndPickerViewType)Type{
    self.tagIndex = Type;
}

- (void)readMemory{
    DoorStatusModel *stautsModel = [RongGaoGit getDoorStatusModel];
    

    
    NSLog(@"self.tagIndex:%ld",(long)self.tagIndex);
    switch (self.tagIndex) {
        case 0:
        case 1:
        case 3:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:

        {
            NSLog(@"%@",[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][self.tagIndex]]);
            
            NSString *str = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][self.tagIndex]] hexadecimalLength:4];
            
            [self.pickerView selectRow:[str intValue] inComponent:0 animated:YES];
            
            break;
        }
        case 2:
        {
            NSString *str = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][self.tagIndex]] hexadecimalLength:4];
            
            
            NSLog(@"%d",[str intValue]-50/5);
            
            int value = ([str intValue]-50)/5;
            [self.pickerView selectRow:value inComponent:0 animated:YES];
            
            break;
        }
            
        case 4:
        {
            NSString *str = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][self.tagIndex]] hexadecimalLength:4];
            
            [self.pickerView selectRow:[str intValue]-1 inComponent:0 animated:YES];

        }
            break;
        case 10:
        {
            NSString *str = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][self.tagIndex]] hexadecimalLength:4];
            int value = ([str intValue]-200)/50;
            
            [self.pickerView selectRow:value inComponent:0 animated:YES];
            
            break;
        }
        case 12:
        case 13:
        {
            
            NSString *str = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][self.tagIndex -1]] hexadecimalLength:4];
            
            [self.pickerView selectRow:[str intValue] inComponent:0 animated:YES];
            
        }
            
        break;

        default:
            
            break;
    }
    
    
}

- (void)sendDataBlueTooth{
    
    NSLog(@"选择的内容%@",self.pickerView.dataArray[0][[self.pickerView selectedRowInComponent:0]]);
    
    DoorSendCodeModel *sendCodeModel = [[DoorSendCodeModel alloc]init];
    
    NSLog(@"%ld===",(long)self.selectIndex);
    
    if ([[sendCodeModel getfunCodeWithIndex:self.selectIndex] isEqualToString:@"100D"]) {
        
        
        NSInteger row=[self.pickerView selectedRowInComponent:0];

        [sendCodeModel repairZero:[NSString stringWithFormat:@"%ld",(long)row]];
        
    }else{
        
        [sendCodeModel repairZero:self.pickerView.dataArray[0][[self.pickerView selectedRowInComponent:0]]];

    }
    
    
    //获取数据码
    
    //拼接code
    NSLog(@"%@==%@",[sendCodeModel getfunCodeWithIndex:self.selectIndex],[sendCodeModel getSendCmd]);
    
    [[AppDelegate globalDelegate] sendCommandByFunctionCode:[sendCodeModel getfunCodeWithIndex:self.selectIndex] functionCommand:[sendCodeModel getSendCmd]];
    [SVProgressHUD showSuccessWithStatus:@"设置成功"];
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - --------------------------数据源--------------------------

- (NSMutableArray *)valueArray{
    
    if (!_valueArray) {
        
        _valueArray = [[NSMutableArray alloc] init];
        
        switch (self.tagIndex) {
            case WWStudyType://0~100
            case WWRunType:
            {
                for (NSInteger i = 0; i< 101; i++) {
                    
                    i = i;
                    [_valueArray addObject:@(i)];
                }
                
                break;
            }
            case WWElectricityType://50~95 5递增
            {
                for (NSInteger i = 0; i < 10; i++) {
                    
                    NSInteger k = (i * 5) + 50;
                    [_valueArray addObject:@(k)];
                }
                
                break;
            }
            case WWDelicacyType://1~5
            {
                for (NSInteger i = 1; i < 6; i ++) {
                    
                    i = i;
                    [_valueArray addObject:@(i)];
                    
                }
                break;
            }
            case WWDoorPointType://0~1
            case WWDelicacyDoorOpenType:
            case WWDelicacyDoorOffType:
            case WWLockType:
            case WWDoorChangeType:
            case WWPIDType:
            {
                for (NSInteger i = 0; i < 2; i ++) {
                    i = i;
                    [_valueArray addObject:@(i)];
                }
                break;
            }
                
            case WWSpeedType://200 ~500  50递增
            {
                for (NSInteger i = 0; i < 7; i++) {
                    
                    NSInteger k = (i * 50) + 200;
                    [_valueArray addObject:@(k)];
                }
                
                break;
            }
                
            case WWDoorWaitType://0 ~ 15
            {
                for (NSInteger i = 0; i < 16; i++) {
                    
                    i = i;
                    [_valueArray addObject:@(i)];
                }
                
                break;
            }
            default:
                break;
        }
        
    }
    
    return _valueArray;
}

- (NSArray *)titleArray{
    
    if (!_titleArray) {
        
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"GatherPlist" ofType:@"plist"];
        _titleArray = [NSArray arrayWithContentsOfFile:plistPath];
        
    }
    return _titleArray;
}

- (NSArray *)DescribeArray{
    
    if (!_DescribeArray) {
        
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"GatherDescribePlist" ofType:@"plist"];
        _DescribeArray = [NSArray arrayWithContentsOfFile:plistPath];
        
    }
    return _DescribeArray;
}

- (GatherHeadView *)headView{
    
    if (!_headView) {
        
        __weak UIView *superView = self.view;
        _headView = [[GatherHeadView alloc] init];
        _headView.backgroundColor = [UIColor colorWithHexString:@"#f9f4e8"];
        [superView addSubview:_headView];
        
    }
    return _headView;
}

- (FIDPickerView *)pickerView{
    
    if (!_pickerView) {
        
        __weak UIView *superView = self.view;
        
        _pickerView = [[FIDPickerView alloc] initWithDataSource:@[self.valueArray] detailSource:nil];
        
        [superView addSubview:_pickerView];
        
    }
    return _pickerView;
}

- (UIButton *)sendBtn{
    
    if (!_sendBtn) {
        
        __weak UIView *superView = self.view;
        _sendBtn = [[UIButton alloc] init];
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn setBackgroundImage:[UIImage imageNamed:@"button_next_nor"] forState:UIControlStateNormal];
        [_sendBtn setTintColor:[UIColor whiteColor]];
        [_sendBtn addTarget:self action:@selector(sendDataBlueTooth) forControlEvents:UIControlEventTouchUpInside];
        [superView addSubview:_sendBtn];
        
    }
    return _sendBtn;
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    __weak typeof(self) weakself = self;
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(superView.mas_top);
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.height.mas_equalTo(pxScaleHeight(260));
    }];
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakself.headView.mas_bottom);
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.height.mas_equalTo(pxScaleHeight(680));
        
    }];
    
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.left.mas_equalTo(superView.mas_left).mas_offset(20);
        make.right.mas_equalTo(superView.mas_right).mas_offset(-20);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-56);
        
    }];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
